package com.campus.counseling.controller;

import com.campus.counseling.model.common.Result;
import com.campus.counseling.model.dto.ScheduleDTO;
import com.campus.counseling.model.dto.BatchScheduleDTO;
import com.campus.counseling.model.query.ScheduleQuery;
import com.campus.counseling.model.vo.ScheduleVO;
import com.campus.counseling.model.vo.TimeSlotVO;
import com.campus.counseling.service.ScheduleService;
import com.campus.counseling.service.AppointmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.time.LocalDate;

@Api(tags = "排班管理")
@RestController
@RequestMapping("/api/counselor/schedule")
@RequiredArgsConstructor
public class ScheduleController {

    private static final Logger log = LoggerFactory.getLogger(ScheduleController.class);

    private final ScheduleService scheduleService;
    private final AppointmentService appointmentService;

    @ApiOperation("获取排班列表")
    @GetMapping("/list")
    public Result<List<ScheduleVO>> list(ScheduleQuery query) {
        log.info("查询排班列表，参数：{}", query);
        if (query.getStartDate() == null) {
            query.setStartDate(LocalDate.now());
        }
        if (query.getEndDate() == null) {
            query.setEndDate(LocalDate.now().plusDays(30));
        }
        List<ScheduleVO> list = scheduleService.getScheduleList(query);
        log.info("查询结果：{}", list);
        return Result.success(list);
    }

    @ApiOperation("添加排班")
    @PostMapping("/add")
    public Result<Void> add(@RequestBody ScheduleDTO scheduleDTO) {
        scheduleService.addSchedule(scheduleDTO);
        return Result.success();
    }

    @ApiOperation("批量添加排班")
    @PostMapping("/batchAdd")
    public Result<Void> batchAdd(@RequestBody BatchScheduleDTO batchScheduleDTO) {
        scheduleService.batchAddSchedule(batchScheduleDTO);
        return Result.success();
    }

    @ApiOperation("更新排班")
    @PutMapping("/update")
    public Result<Void> update(@RequestBody ScheduleDTO scheduleDTO) {
        scheduleService.updateSchedule(scheduleDTO);
        return Result.success();
    }

    @ApiOperation("删除排班")
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        scheduleService.deleteSchedule(id);
        return Result.success();
    }

    @GetMapping("/available-slots")
    @ApiOperation(value = "获取可预约时间段")
    public Result<List<TimeSlotVO>> getAvailableTimeSlots(
        @RequestParam("counselorId") Long counselorId,
        @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date
    ) {
        List<TimeSlotVO> slots = scheduleService.getAvailableTimeSlots(counselorId, date);
        return Result.success(slots);
    }
} 